package de.codi.xtest;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import de.codi.CODISettings;
import de.codi.exception.CodiException;
import de.codi.wrapper.Main;
import de.unima.alcomox.exceptions.AlcomoException;
import de.unima.alcomox.mapping.Characteristic;
import de.unima.alcomox.mapping.Mapping;
import eu.sealsproject.platform.repos.tdrs.client.TestCaseIterator;
import eu.sealsproject.platform.repos.tdrs.client.TestDataSuite;


/**
 * 
 * Shows how to use the SEALS TDRS (test data respository service) Can be used
 * for running experiments on full sets of alignments.
 * 
 */
public class SealsTDRS_finalTest
{

    public static String tmp;


    public static void main(String[] args) throws CodiException, AlcomoException, IOException, URISyntaxException
    {
        int track = 1; // 0 = Anatomy, 1 = Benchmark (1,2), 2 = Conference, 3 = multifarm

        StringBuilder sb = new StringBuilder();

        String testCollectionName = "";
        String testCollectionVersion = "";

        switch(track) {
            case 0:
                testCollectionName = "Anatomy Testsuite";
                testCollectionVersion = "2010";
            break;
            case 1:
                testCollectionName = "Benchmark Testsuite";
                testCollectionVersion = "2010full";
                /*
                // benchmark 2:
                testCollectionName = "b50cbebb-5be3-4fc6-a02d-3a801791770c";
                testCollectionVersion = "ebee3a34-a155-4d50-879f-33372822db4d";
                CODISettings.SEALS_TDRS_URL = "http://seals-test.sti2.at/tdrs-web/";

                // benchmark 3:
                testCollectionName = "81a7f2d2-81fd-4a6d-b076-3adc7b3e0918";
                testCollectionVersion = "8372414c-0185-4ff0-921b-969620c04777";
                CODISettings.SEALS_TDRS_URL = "http://seals-test.sti2.at/tdrs-web/";
                */
            break;
            case 2:
                testCollectionName = "Conference Testsuite";
                testCollectionVersion = "2010";
            break;
            case 3:
                testCollectionName = "de-fr-multifarm"; 
                testCollectionVersion = "de-fr-multifarm-v1.0";
                CODISettings.SEALS_TDRS_URL = "http://seals-test.sti2.at/tdrs-web/";
                break;
        }

        Characteristic cAfter = new Characteristic();
        double precA = 0;
        double fmeasureA = 0;
        double recallA = 0;

        int zaehler = 0;

        TestDataSuite tds = new TestDataSuite(CODISettings.SEALS_TDRS_URL, testCollectionName, testCollectionVersion);
        TestCaseIterator it = tds.getTestCases();

        while(it.hasNext()) {
            String testCaseId = it.next();
            // if(!(testCaseId.equals("iasted-sigkdd"))) {
            // continue;
            // }
            System.out.println("testcase with id: " + testCaseId);
            System.out.println("\tcomponent source:    " + tds.getDataItem(testCaseId, "source"));
            System.out.println("\tcomponent target:    " + tds.getDataItem(testCaseId, "target"));
            System.out.println("\tcomponent reference: " + tds.getDataItem(testCaseId, "reference"));

            // load reference alignment
            Mapping reference = new Mapping(fetchAndStore(tds.getDataItem(testCaseId, "reference"), "reference").getAbsolutePath());

            Main matcher = new Main();
            URI alignment = matcher.align(tds.getDataItem(testCaseId, "source").toURI(), tds.getDataItem(testCaseId, "target").toURI());

            // if(true)
            // continue;
            // compute precision and recall
            Characteristic after = new Characteristic(new Mapping(alignment.getPath()), reference);
            cAfter.join(after);
            System.out.println("\nAfter debugging: \n" + after);

            sb.append("\n" + testCaseId + "\t" + tmp + "\t" + after.getPrecision() + "\t" + after.getRecall());

            // mikro
            zaehler++;
            precA += after.getPrecision();
            recallA += after.getRecall();
            fmeasureA += after.getFMeasure();
        }
        System.out.println("\n\n\n-----------------Makro-----------------------------");
        System.out.println("After debugging: \n" + cAfter);
        System.out.println("\n\n\n-----------------Mikro-----------------------------");
        System.out.println("\nAfter debugging:");
        System.out.println("Precision:" + precA / zaehler);
        System.out.println("Recall:" + recallA / zaehler);
        System.out.println("F-Measure:" + fmeasureA / zaehler);
        System.out.println("------------------------------------------\n\n\n");

        // System.out.println(sb);
    }


    public static File fetchAndStore(URL url, String type) throws IOException
    {
        File f = File.createTempFile(type, ".rdf");
        PrintWriter writer = new PrintWriter(f);
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine;
        while((inputLine = in.readLine()) != null)
            writer.println(inputLine);
        in.close();
        writer.flush();
        writer.close();
        return f;
    }
}
